package com.tigercar.servicemanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tigercar.common.pojo.PageResult;
import com.tigercar.servicemanagement.mapper.DeptMapper;
import com.tigercar.servicemanagement.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class DeptService {
    @Autowired
    private DeptMapper deptMapper;

    public PageResult<Dept> searchDeptList(Long curPage, Long pageSize, Integer id, String name) {
        IPage<Dept> iPage = new Page<>(curPage, pageSize);
        QueryWrapper<Dept> queryWrapper = Wrappers.query();
        if(StringUtils.isNotEmpty(name)){
            queryWrapper.like("dept_name",name);
        }
        if(id!=null){
            queryWrapper.eq("dept_id",id);
        }

        iPage = deptMapper.selectPage(iPage,queryWrapper);

        List<Dept> deptList = iPage.getRecords();
        PageResult<Dept> pageResult = new PageResult<>(iPage.getTotal(),iPage.getPages(),deptList);
        return pageResult;
    }

    public void addDept(Integer deptId, String deptName) {
        Dept dept = new Dept();
        dept.setDeptId(deptId);
        dept.setDeptName(deptName);
        deptMapper.insert(dept);
    }

    public void updateDept(Integer deptId, String deptName) {
        Dept dept = new Dept();
        dept.setDeptId(deptId);
        dept.setDeptName(deptName);
        deptMapper.updateById(dept);
    }

    public void deleteDept(Integer deptId) {
        deptMapper.deleteById(deptId);
    }
}
